﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

/*
 * ReadOnlyDrawer
 *
 *
 *
 * 依赖：
 *
 * ！不写重复代码！
 * @author TTun
 * @date 8/31/2017 9:23:54 PM
 */

namespace TTGameEngine {

[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
public class ReadOnlyDrawer : PropertyDrawer {
    public override float GetPropertyHeight(SerializedProperty property,
                                            GUIContent label) {
        return EditorGUI.GetPropertyHeight(property, label, true);
    }

    public override void OnGUI(Rect position,
                               SerializedProperty property,
                               GUIContent label) {
        //防止[ReadOnly]嵌套。
        bool oldEnabled = GUI.enabled;
        GUI.enabled = false;
        EditorGUI.PropertyField(position, property, label, true);
        GUI.enabled = oldEnabled;
    }
}
[CustomPropertyDrawer(typeof(WriteEnabledAttribute))]
public class WriteEnabledDrawer : PropertyDrawer {
    public override float GetPropertyHeight(SerializedProperty property,
                                            GUIContent label) {
        return EditorGUI.GetPropertyHeight(property, label, true);
    }

    public override void OnGUI(Rect position,
                               SerializedProperty property,
                               GUIContent label) {
        //防止[ReadOnly]嵌套。
        bool oldEnabled = GUI.enabled;
        GUI.enabled = true;
        EditorGUI.PropertyField(position, property, label, true);
        GUI.enabled = oldEnabled;
    }
}
[CustomPropertyDrawer(typeof(TextAreaAttribute))]
public class TextAreaDrawer : PropertyDrawer {
    public override float GetPropertyHeight(SerializedProperty property,
                                            GUIContent label) {
        return 50;
    }
    public override void OnGUI(Rect position,
                               SerializedProperty property,
                               GUIContent label) {
        EditorGUI.LabelField(position, label);
        var TheRect = GUILayoutUtility.GetRect(label, GUIStyle.none);
        property.stringValue = EditorGUI.TextArea(position, property.stringValue);
    }
}

}